//
//  ByPersonalVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/18.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByPersonalVC.h"

/** 控制器 */
#import "ByLoginVC.h"                   // 登陆
#import "ByPersonalDataVC.h"            // 个人资料
#import "ByPersonalMyHandVC.h"          // 我投过的
#import "ByPersonalSettingVC.h"         // 设置

/** 视图 */
#import "ByPersonalHeaderView.h"        // 头视图

@interface ByPersonalVC () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) ByPersonalHeaderView *headerView;

@property (nonatomic, copy) NSArray *subjects;


@end

@implementation ByPersonalVC

#pragma mark - getter
- (NSArray *)subjects {
    if (!_subjects) {
        _subjects = @[@"个人资料", @"我投过的", @"设置"];
    }
    return _subjects;
}
- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[ByPersonalHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    }
    return _headerView;
}

#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"个人中心";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    /** 设置导航栏为半透明 状态栏为白色 */
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
    
    if (BY_USER) {
        /** 刷新头视图 */
        self.headerView.nickLabel.text = BY_USER.nickname;
        NSLog(@"%@",BY_USER.head);
        [self.headerView.iconImgView setImageWithURL:[NSURL URLWithString:BY_USER.head] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
        
        if (BY_USER.user_roles.integerValue == BY_USER_ROLE_STUDENT) { // 角色是学生
            self.subjects = @[@"个人资料", @"我投过的", @"设置"];
        } else {
            self.subjects = @[@"个人资料", @"我收到的", @"设置"];
        }
        [self.table reloadData];
        
    } else {
        self.headerView.nickLabel.text = @"请登录";
        [self.headerView.iconImgView setImageWithURL:[NSURL URLWithString:BY_USER.head] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
        
        self.subjects = @[@"个人资料", @"设置"];
        [self.table reloadData];
    }
}



#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    /** 头视图 */
    UIView *view = [[UIView alloc] initWithFrame:self.headerView.bounds];
    [view addSubview:self.headerView];
    self.table.tableHeaderView = view;
    
}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.subjects.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = FONT(14);
    }
    
    cell.textLabel.text = self.subjects[indexPath.section];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.5;
    }
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        ByPersonalDataVC *vc = [[ByPersonalDataVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *title = cell.textLabel.text;
        if ([title isEqualToString:@"我投过的"]) { // 我投过的
            if (!BY_USER) { // 如果未登录，则先去登录
                ByLoginVC *vc = [[ByLoginVC alloc] init];
                ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
                [self presentViewController:navi animated:YES completion:nil];
                return;
            }
            ByPersonalMyHandVC *vc = [[ByPersonalMyHandVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } else if ([title isEqualToString:@"我收到的"]) { // 我收到的
            [UIAlertView showWithTitle:nil message:@"请登录电脑端网站查看" cancelButtonTitle:nil otherButtonTitles:@[@"确定"] tapBlock:nil];
        } else {
            ByPersonalSettingVC *vc = [[ByPersonalSettingVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }

}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offSetY = scrollView.contentOffset.y;
    
    if (offSetY <= 0) {
        // 往下拉，头部视图放大
        CGFloat bgScale = -(offSetY / self.headerView.height) + 1;
       
        // 设置背景可拉伸
        self.headerView.bgImgView.transform = CGAffineTransformMakeScale(1, bgScale);
        self.headerView.bgImgView.top = offSetY;
    }
    else {
        
    }
}


#pragma mark - 点击事件

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    NSLog(@"%@ memory warning", [self class]);
}



@end
